package work;

import java.io.FileReader;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.Properties;
import java.util.Scanner;

public class Menu {

    public static void main(String[] args) throws Exception{
        Properties properties = new Properties();
        properties.load(new FileReader("C:\\Users\\星星在唱歌\\Desktop\\学习\\LIU\\day23\\src\\bankDate.properties"));
        Class<?> bankClass = Class.forName(properties.getProperty("BankClassName"));
        Class<?> UserClass = Class.forName(properties.getProperty("UserClassName"));
        User user = (User) UserClass.newInstance();
        BankSystem banksystem = (BankSystem)bankClass.newInstance();
        Method openUser = UserClass.getMethod(properties.getProperty("openUserMethod"),bankClass,double.class);
        Method peo = UserClass.getMethod(properties.getProperty("peoMethod"),bankClass,String.class,String.class);
        Method inputMenoy = UserClass.getMethod(properties.getProperty("inputMoneyMethod"),bankClass,String.class,String.class,double.class);
        Method outputMenoy = UserClass.getMethod(properties.getProperty("outputMoneyMethod"),bankClass,String.class,String.class,double.class);

        Field falg = bankClass.getField(properties.getProperty("flag"));
        System.out.println("---------欢迎来到，张三-----------");
        Scanner sc = new Scanner(System.in);
        int num = 0;

        while (true){
            System.out.println("*****************************");
            System.out.println("        欢迎来到管理系统        ");
            System.out.println("        **************       ");
            System.out.println("    1.开户        2.存钱        ");
            System.out.println("    3.取钱        4.退出        ");
            System.out.print("请输入选项：");
            num = sc.nextInt();
            if (num == 1){
                Boolean isflag = (boolean)falg.get(banksystem);
                if (isflag){
                    try {
                        throw new Allexception("当前账户已存在");
                    } catch (Allexception e) {
                        e.printStackTrace();
                    }
                }else {
                    System.out.println("开户需要10元，请输入开户金额");
                    double openMen;
                    double d = 100000000;
                    while (d != 0){
                        openMen = sc.nextInt();
                        d = (double)openUser.invoke(user,banksystem,openMen);
                    }
                    System.out.print("请输入要注册的用户名：");
                    String admin = sc.next();
                    System.out.print("请输入要注册密码：");
                    String pwd = sc.next();
                    peo.invoke(user,banksystem,admin,pwd);
                }
            }
            else
            if (num == 2){
                Boolean isflag = (boolean)falg.get(banksystem);
                if (!isflag){
                    try {
                        throw new Allexception("当前账户不存在，请按“ 1 ” 开户");
                    } catch (Allexception e) {
                        e.printStackTrace();
                    }
                }else {
                    System.out.print("请输入账号：");
                    String admin = sc.next();
                    System.out.println();
                    System.out.print("请输入密码：");
                    String pwd = sc.next();
                    System.out.println();
                    System.out.print("请输入存钱的金额：");
                    double money = sc.nextInt();
                    inputMenoy.invoke(user,banksystem,admin,pwd,money);
                }
            }
            else
            if (num == 3){
                Boolean isflag = (boolean)falg.get(banksystem);
                if (!isflag){
                    try {
                        throw new Allexception("当前账户不存在，请按“ 1 ” 开户");
                    } catch (Allexception e) {
                        e.printStackTrace();
                    }
                }else {
                    System.out.print("请输入账号：");
                    String admin = sc.next();
                    System.out.println();
                    System.out.print("请输入密码：");
                    String pwd = sc.next();
                    System.out.println();
                    System.out.print("请输入取钱的金额：");
                    double money = sc.nextInt();
                    outputMenoy.invoke(user,banksystem,admin,pwd,money);
                }
            }
            else
            if (num == 4){
                System.exit(0);
            }
            else {
                try {
                    throw new Allexception("输入有误，请重新输入！！");
                } catch (Allexception e) {
                    e.printStackTrace();
                }
            }
        }

    }
}
